import 'package:flutter/material.dart';
import 'package:flutter_ahj_app/app/yhpc/YhpcViewModel.dart';
import 'package:flutter_ahj_app/base/basewidget/view/BaseWidget.dart';
import 'package:flutter_ahj_app/base/utils/JUIKit/JText.dart';
import 'package:flutter_ahj_app/base/utils/StringUtils.dart';
import 'package:flutter_ahj_app/base/utils/ToastUtils.dart';
import 'package:flutter_ahj_app/base/utils/mTopBar.dart';
import 'package:flutter_ahj_app/base/values/SQColor.dart';
import 'package:flutter_ahj_app/base/values/dimens.dart';
import 'package:flutter_ahj_app/base/widget/LoadingWidget.dart';
import 'package:flutter_ahj_app/base/widget/NoneData.dart';

class SelectPcnr extends BaseStatefulWidget {
  final String selpcId;
  SelectPcnr(this.selpcId);

  @override
  State<StatefulWidget> initState() {
    // TODO: implement initState
    // throw UnimplementedError();
    return SelectPcnrState();
  }
}

class SelectPcnrState extends BaseState<YhpcViewModel, SelectPcnr> {
  List _pcnrList;
  List _selPcnrList = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    Future.delayed(Duration.zero, () {
      _getPcnrData();
    });
  }

  // 获取排查内容
  _getPcnrData() {
    // List riskIdList = [];
    // for (var i = 0; i < widget.selPcdxList.length; i++) {
    //   riskIdList.add(widget.selPcdxList[i]['id']);
    // }
    var map = {'riskId': StringUtils.getStr(widget.selpcId), 'current': '1', 'size': '9999'};
    viewModel.getPcnrData(map);
    viewModel.getPcnrDataStream().listen((event) {
      if (event['code'] == '0') {
        setState(() {
          _pcnrList = [];
          _pcnrList = event['data']['records'];
          for (var i = 0; i < _pcnrList.length; i++) {
            _pcnrList[i]['selControlMeasureList'] = {
              'isSel': true,
              'name': _pcnrList[i]['controlMeasureList'][0]
            };
            _selPcnrList.add(_pcnrList[i]);
          }
        });
      } else {
        ToastUtils.showToast(event['msg']);
      }
    }, onError: (e) {
      print(e);
    });
  }

  @override
  Widget initWidget() {
    return Scaffold(
      backgroundColor: Color(0xfff4f5f6),
      appBar: MTopBar(
        elevation: 0,
        title: '选择排查内容',
        rightButtonText: '确定',
        onRightButtonClick: () {
          for (var i = 0; i < _selPcnrList.length; i++) {
            Map map = _selPcnrList[i]['selControlMeasureList'];
            if (map['isSel'] == true) {
              Navigator.pop(context, _selPcnrList);
              return;
            }
          }
          ToastUtils.showToast('至少选择一条排查内容');
        }
      ).create(),
      body: _pcnrList == null ? LoadingWidget() : _pcnrList.length == 0 ? NoneData() : _bodyView(),
    );
  }

  _bodyView() {
    return Container(
      margin: EdgeInsets.all(15),
      child: SingleChildScrollView(
        child: ListView.builder(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            itemCount: _pcnrList.length,
            itemBuilder: (context, index) {
              return InkWell(
                onTap: () {
                  Navigator.pushNamed(context, '/PcnrView', arguments: _pcnrList[index]['selControlMeasureList']).then((value) {
                    if (value != null) {
                      setState(() {
                        _pcnrList[index]['selControlMeasureList'] = value;
                        _selPcnrList[index]['selControlMeasureList'] = value;
                      });
                    }
                  });
                },
                child: Column(
                  children: [
                    Container(
                      decoration: BoxDecoration(
                        color: SQColor.white,
                        borderRadius: BorderRadius.circular(8),
                      ),
                      padding: EdgeInsets.all(15),
                      child: Row(
                        children: [
                          Expanded(
                            child: JText(_pcnrList[index]['dangerSourceName'] ?? ''),
                          ),
                          JText(
                            _pcnrList[index]['selControlMeasureList']['isSel'] ? '已全选' : '不涉及',
                            color: Color(0xff999999),
                          ),
                          Icon(
                            Icons.arrow_forward_ios_outlined,
                            size: Dimens.dimens15,
                            color: SQColor.black_999999,
                          ),
                        ],
                      ),
                    ),
                    Container(
                      height: 10,
                      color: Color(0xfff6f6f6),
                      width: double.infinity,
                    ),
                  ],
                ),
              );
            }),
      ),
    );
  }
}
